package com.gitee.ice1938.module.admin.app.manage;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.jdbc.AbstractBaseDataSourceInitializer;
import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

/**
 */
@Configuration
//@ComponentScan("com.gitee.ice1938.module.admin.app.manage")
@AutoConfigurationPackage
public class AdminAppManageModuleConfig implements InitializingBean {

	@Autowired
	private MessageSource messageSource;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		// 根据模块信息添加i18n 默认配置 MessageSourceAutoConfiguration会根据是否配置了前缀进行资源相关类加载
		ResourceBundleMessageSource res = (ResourceBundleMessageSource) messageSource;
		res.addBasenames("com/gitee/ice1938/module/admin/app/manage/messages");
	}
	@Bean("com_gitee_ice1938_module_admin_app_manage_initializer")
	public AbstractDataSourceInitializer moduleDataSourceInitializer(DataSource dataSource,
	        ResourceLoader resourceLoader) {
		return new AbstractBaseDataSourceInitializer(dataSource, resourceLoader) {
			@Override
			protected String getValidSql() {
				return "select * from app_version_detail";
			}
			@Override
			protected String getSchemaLocation() {
				return "classpath:db/app_manage_ddl.sql";
			}

			protected void customize(ResourceDatabasePopulator populator) {
				populator.addScript(resourceLoader.getResource("classpath:db/app_manage_init_data.sql"));
			}
		};
	}
}
